﻿using SuperMap.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MDF
{
    class DataCheck
    {

        private Datasources m_datasources;
        /// <summary>
        /// 已匹配的数据集
        /// </summary>
        public List<DatasetVector[]> m_SetToSet;

        /// <summary>
        /// 数据检查初始化
        /// </summary>
        public DataCheck()
        {

        }
        /// <summary>
        /// 数据检查初始化
        /// </summary>
        /// <param name="datasources">数据源集合</param>
        public DataCheck(Datasources datasources)
        {
            m_datasources = datasources;
        }
        /// <summary>
        /// 检查匹配相关信息
        /// </summary>
        /// <returns></returns>
        public string CheckMatchInfo(bool Checked,string set1Name, string set2Name)
        {
            try
            {
                //结果记录
                StringBuilder stringBuilder = new StringBuilder();
                //判断是否为选择集
                if (Checked)
                {
                    //进行选择集检查
                    //实例化选择集xml读写对象
                    IO_xml s_xml = new IO_xml("SelectionData");
                    //获取选择集中的数据集的名字
                    List<string[]> selections1 = s_xml.ReadChildValues(set1Name);
                    List<string[]> selections2 = s_xml.ReadChildValues(set2Name);
                    //获取数据集信息
                    List<DatasetVector> datasetVector1s = new List<DatasetVector>();
                    List<DatasetVector> datasetVector2s = new List<DatasetVector>();
                    //判断选择集中数据集是否存在
                    foreach (string[] setInfo in selections1)
                    {
                        //判断当前的数据源是否有对应数据
                        if (m_datasources[setInfo[1]] != null)
                        {
                            if (m_datasources[setInfo[1]].Datasets[setInfo[0]] != null)
                            {
                                datasetVector1s.Add(m_datasources[setInfo[1]].Datasets[setInfo[0]] as DatasetVector);
                            }
                            else
                            {
                                stringBuilder.AppendLine("当前数据源缺少数据集：" + setInfo[0]);
                            }
                        }
                        else
                        {
                            stringBuilder.AppendLine("当前缺少数据源：" + setInfo[1]);
                        }
                    }
                    foreach (string[] setInfo in selections2)
                    {
                        //判断当前的数据源是否有对应数据
                        if (m_datasources[setInfo[1]] != null)
                        {
                            if (m_datasources[setInfo[1]].Datasets[setInfo[0]] != null)
                            {
                                datasetVector2s.Add(m_datasources[setInfo[1]].Datasets[setInfo[0]] as DatasetVector);
                            }
                            else
                            {
                                stringBuilder.AppendLine("当前数据源缺少数据集：" + setInfo[0]);
                            }
                        }
                        else
                        {
                            stringBuilder.AppendLine("当前缺少数据源：" + setInfo[1]);
                        }
                    }
                    //判断类型是否一致
                    if (datasetVector1s.Count != 0 && datasetVector2s.Count != 0)
                    {
                        string result = CheckSet(datasetVector1s, datasetVector2s);
                        if (result != null)
                        {
                            stringBuilder.AppendLine(result);
                        }
                    }
                    else
                    {
                        stringBuilder.AppendLine("未获取到相关数据集信息！");
                    }
                }
                else
                {
                    //进行数据集检查
                    //遍历数据源，获取数据集信息
                    List<DatasetVector> datasetVector1s = new List<DatasetVector>();
                    List<DatasetVector> datasetVector2s = new List<DatasetVector>();
                    foreach (Datasource datasource in m_datasources)
                    {
                        if (datasource.Datasets[set1Name] != null)
                        {
                            datasetVector1s.Add(datasource.Datasets[set1Name] as DatasetVector);
                        }
                        if (datasource.Datasets[set2Name] != null)
                        {
                            datasetVector2s.Add(datasource.Datasets[set2Name] as DatasetVector);
                        }
                    }
                    //判断类型是否一致
                    if (datasetVector1s.Count != 0 && datasetVector2s.Count != 0)
                    {
                        string result = CheckSet(datasetVector1s, datasetVector2s);
                        if (result != null)
                        {
                            stringBuilder.AppendLine(result);
                        }
                    }
                    else
                    {
                        stringBuilder.AppendLine("未获取到相关数据集信息！");
                    }

                }
                //返回结果
                return stringBuilder.ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 检查数据类型是否一致
        /// </summary>
        /// <param name="datasetVector1s">数据集1类型列表</param>
        /// <param name="datasetVector2s">数据集2类型列表</param>
        /// <returns>完全匹配返回1，部分匹配返回-1，不匹配返回0</returns>
        private string CheckSet( List<DatasetVector> datasetVector1s, List<DatasetVector> datasetVector2s)
        {
            //统计总共有多少个数据集
            int count = datasetVector1s.Count + datasetVector2s.Count;
            //实例化 数据集对 列表
            m_SetToSet = new List<DatasetVector[]>();
            //匹配类型的相同的数据集
            foreach (DatasetVector setVector1 in datasetVector1s)
            {
                foreach (DatasetVector setVector2 in datasetVector2s)
                {
                    if (setVector1.Type == setVector2.Type)
                    {
                        //检查后记录配对信息
                        m_SetToSet.Add(new DatasetVector[] { setVector1, setVector2 });
                    }
                }
            }
            //删除匹配好的数据集,来判断选择据是否完全匹配
            foreach (DatasetVector[] setVectors in m_SetToSet)
            {
                if (datasetVector1s.Contains(setVectors[0]))
                {
                    datasetVector1s.Remove(setVectors[0]);
                }
                if (datasetVector2s.Contains(setVectors[1]))
                {
                    datasetVector2s.Remove(setVectors[1]);
                }
            }
            //判断结果
            if (datasetVector1s.Count + datasetVector2s.Count == 0)
            {
                //返回结果,完全匹配
                return null;
            }
            else if (datasetVector1s.Count + datasetVector2s.Count < count)
            {
                //返回结果，部分匹配
                return "选择集合中数据集未能完全配对！\n但您仍可以继续计算已配对数据集！\n";
            }
            else
            {
                //返回结果，不匹配
                return "当前数据选择不合法！\n数据集合中无相互对应类型数据存在！\n";
            }
        }
        
        public string CheckYuzhiInfo(int[] modeFuns)
        {
            try
            {
                //记录结果
                StringBuilder stringBuilder = new StringBuilder();
                foreach (int mode in modeFuns)
                {
                    switch (mode)
                    {
                        case 1:
                            //检查空间阈值
                            foreach (DatasetVector[] set12 in m_SetToSet)
                            {
                                string result = CheckKongJianInfo(set12[0].Name, set12[1].Name);
                                if (result != null)
                                {
                                    stringBuilder.AppendLine(result);
                                }
                            }
                            break;
                        case 2:
                            //检查属性映射
                            foreach (DatasetVector[] set12 in m_SetToSet)
                            {
                                string result = CheckYingSheInfo(set12[0].Name, set12[1].Name);
                                if (result != null)
                                {
                                    stringBuilder.AppendLine(result);
                                }
                            }
                            break;
                        case 3:
                            //检查拓扑阈值
                            foreach (DatasetVector[] set12 in m_SetToSet)
                            {
                                string result = CheckTopologyInfo(set12);
                                if (result != null)
                                {
                                    stringBuilder.AppendLine(result);
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
                return stringBuilder.ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 检查阈值信息
        /// </summary>
        /// <param name="set1Name">数据集1的名称</param>
        /// <param name="set2Name">数据集2的名称</param>
        /// <returns>返回对结果的描述字符串</returns>
        private string CheckKongJianInfo(string set1Name, string set2Name)
        {
            try
            {
                //记录结果
                StringBuilder stringBuilder = new StringBuilder();
                //空间阈值
                IO_xml m_Xml = new IO_xml("PiPeiYuZhi");
                string[] m_keys = new string[] { "DistanceThershold", "LengthThershold" };
                //数据节点名称为两个数据的名称
                string dataName = set1Name + ".." + set2Name;
                string dataNameRe = set2Name + ".." + set1Name;
                //查询是否有空间阈值信息
                if (m_Xml.ReadValueByKey(dataName, m_keys[0]) == null && m_Xml.ReadValueByKey(dataNameRe, m_keys[0]) == null)
                {
                    return set1Name + " : " + set2Name + "不存在空间阈值信息！";
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 检查映射信息
        /// </summary>
        /// <param name="set1Name">数据集1的名称</param>
        /// <param name="set2Name">数据集2的名称</param>
        /// <returns>返回对结果的描述字符串</returns>
        private string CheckYingSheInfo(string set1Name, string set2Name)
        {
            try
            {
                //空间阈值
                IO_xml m_Xml = new IO_xml("ShuXingPiPei");
                //数据节点名称为两个数据的名称
                string dataName = set1Name + ".." + set2Name;
                string dataNameRe = set2Name + ".." + set1Name;
                //判断是否存在映射信息
                if (m_Xml.ReadChildNodesName(dataName).Length == 0 && m_Xml.ReadChildNodesName(dataNameRe).Length == 0)
                {
                    return set1Name + " : " + set2Name + "不存在映射信息！";
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 检查拓扑阈值和链接信息
        /// </summary>
        /// <param name="set12">数据集12的集合</param>
        /// <returns></returns>
        private string CheckTopologyInfo(DatasetVector[] set12)
        {
            try
            {
                //记录结果
                StringBuilder stringBuilder = new StringBuilder();
                //拓扑阈值
                IO_xml m_Xml = new IO_xml("PiPeiYuZhi");
                string[] m_keys = new string[] { "ErrorThershold", "ToleranceThershold", "DepthThershold" };
                //数据节点名称为两个数据的名称
                string dataName = set12[0].Name + "..." + set12[1].Name;
                string dataNameRe = set12[1].Name + "..." + set12[0].Name;
                if (m_Xml.ReadValueByKey(dataName, m_keys[0]) == null && m_Xml.ReadValueByKey(dataNameRe, m_keys[0]) == null)
                {
                    stringBuilder.AppendLine(set12[0].Name + " : " + set12[1].Name + "不存在拓扑阈值信息！");
                }
                //查询是否有链接信息
                if (set12[0].Type == DatasetType.Point)
                {
                    if (m_Xml.ReadValues("TuoPuLink", set12[0].Name).Length == 0)
                    {
                        stringBuilder.AppendLine(set12[0].Name + "不存在连接信息！");
                    }
                }
                if (set12[1].Type == DatasetType.Point)
                {
                    if (m_Xml.ReadValues("TuoPuLink", set12[1].Name).Length == 0)
                    {
                        stringBuilder.AppendLine(set12[1].Name + "不存在连接信息！");
                    }
                }
                //返回结果
                return stringBuilder.ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
